<?php

namespace app\api\controller\web;


use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptuan;
use app\admin\model\Shopwifi;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userwithdraw;
use app\common\controller\Api;
use OSS\OssClient;
use think\Config;

/**
 *
 */
class Shop extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function getCityList()
    {
        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['org_id'] = $org['id'];
        $shopModel = new \app\admin\model\City();
        $list = $shopModel->where($where)->select();

        $this->success('请求成功', $list);
    }


    public function update(){
        $id = $this->request->param('id');
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $agentMobile = $this->request->param('agent_mobile');
        $ratio = $this->request->param('ratio');
        $mt_name = $this->request->param('mt_name');
        $agent_ratio = $this->request->param('agent_ratio');
        $logo = $this->request->param('logo');
        $dou_id = $this->request->param('dou_id');
        $poi_id = $this->request->param('poi_id');

        if($mobile == ""){
            $this->error("店铺手机号不能为空");
        }
        $userModel = new \app\admin\model\User();
        $shopUser = $userModel->where('mobile',$mobile)->find();
        $db = array();
        if(!$shopUser) $this->error("此手机号不存在");
        if($agentMobile != ""){
            $agentUser = $userModel->where('mobile',$agentMobile)->find();
            if(!$agentUser) $this->error("代理手机号不存在");
            $db['agent_id'] = $agentUser['id'];
        }
        $db['mobile'] = $mobile;
        $db['user_id'] = $shopUser['id'];
        if($name != ""){
            $db['name'] = $name;
        }
        $db['logo'] = $logo;

        if($ratio != ""){
            $db['ratio'] = $ratio;
        }

        if($agent_ratio != ""){
            $db['agent_ratio'] = $agent_ratio;
        }
        if($mt_name != ""){
            $db['mt_name'] = $mt_name;
        }
        if($dou_id){
            $db['dou_id'] = $dou_id;
        }
        if($poi_id){
            $db['poi_id'] = $poi_id;
        }
        $shopModel = new \app\admin\model\Shop();
        $shopModel->where('id',$id)->update($db);
        $this->success('成功');

    }


    public function detail(){

        $id = $this->request->param('id');
        $shopDetail = new \app\admin\model\Shop();
        $shop = $shopDetail->where('id',$id)->find();
        $userModel = new \app\admin\model\User();
        $user = $userModel->where('id',$shop['agent_id'])->find();
        $shop['agentMobile']  = "";
        if($user){
            $shop['agentMobile']  = $user['mobile'];
        }
        $shop['logoPath'] = $shop['logo'];
        $shop['logo'] = cdnurl($shop['logo'],true);

        $cover = explode(",",$shop['cover']);

        $covers = array();
        foreach ($cover as $key=>$val){
            if($val != ""){
                $covers[] = cdnurl($val,true);
            }
        }
        $shop['cover'] =  $covers;

        $this->success('',$shop);
    }

    /**
     * 店铺列表接口
     *
     */
    public function getList()
    {
        $user = $this->auth->getUser();
        $keyword = $this->request->param('keyword');
        $city = $this->request->param('city');

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['Shop.org_id'] = $org['id'];
        if($keyword != ""){
            $where['name'] =array('like', "%" . $keyword . "%");
        }
        if($city != ""){
            $where['city'] =$city;

        }


        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $shopModel = new \app\admin\model\Shop();
        $list = $shopModel->with(['agent'])->limit($offset,$limit)->where($where)->select();
        $total = $shopModel->where('org_id',$org['id'])->count('id');

        foreach ($list as $key => $val) {
            $list[$key]['logo'] = cdnurl($val['logo'], true);
            $list[$key]['agent_name'] = "";
            if($val['agent'] != null){
                $list[$key]['agent_mobile'] = $val['agent']['mobile'];
            }
        }
        $ret = array();
        $ret['total'] =$total;
        $ret['list'] =$list;

        $this->success('请求成功', $ret);
    }

    public function getAllList()
    {
        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['org_id'] = $org['id'];
        $shopModel = new \app\admin\model\Shop();
        $list = $shopModel->where($where)->select();



        $this->success('请求成功', $list);
    }


    /**
     * 提现列表
     */
    public function withList(){

        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;


        $userWithModel = new Userwithdraw();
        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['Shop.org_id'] = $org['id'];


        $list =  $userWithModel->with(['shop'])->where('type','shop')->where($where)->limit($offset,$limit)->order('create_time','desc')->select();
        foreach ($list as $key=>$val){
            $list[$key]['shop_name'] = $val['shop']['name'];
            if($val['status'] == 'doing'){
                $list[$key]['status'] = "处理中";
            }else if($val['status'] == 'success'){
                $list[$key]['status'] = "处理成功";
            }
        }
        $total =  $userWithModel->where('type','shop')->count('id');

        $ret = array();
        $ret['list'] = $list;
        $ret['total'] = $total;


        $this->success('',$ret);
    }

    public function updateWithStatus(){


        $id = $this->request->param('id');
        $withModel = new Userwithdraw();
        $withModel->where('id',$id)->update(['status'=>'success']);

        $this->success('成功');
    }

    public function addBalance(){

        $shop_id = $this->request->param('shop_id');
        $amount = $this->request->param('amount');
        $mobile = $this->request->param('mobile');

        if($amount == "") {
            $this->error("请填写添加的金额");
        }
        if($mobile == ""){
            $this->error("请输入添加手机号");
        }

        $user = $this->auth->getUser();
        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $shopModel = new \app\admin\model\Shop();
        $shop = $shopModel->where('id',$shop_id)->find();
        if($shop['org_id'] != $org['id']) $this->error("无权限添加此店铺");
        $userModel = new \app\admin\model\User();
        $addUser = $userModel->where('org_id',$org['id'])->where('mobile',$mobile)->find();

        $db = array();
        $db['user_id'] = $addUser['id'];
        $db['type'] = "in";
        $db['shop_id'] = $shop_id;

        $db['amount'] = $amount;
        $db['mark'] = "后台管理员添加";
        $db['create_time'] = time();

        $db['b_type'] ="order";
        $db['order_id'] =rand(100000,99999999);


        $userBalanceModel = new Userbalance();
        $userBalanceModel->insert($db);
        $this->success('');
    }

    public function balanceList(){

        $page = $this->request->param('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;
        $user_id = $this->request->param('user_id');
        $balanceModel = new Userbalance();
        $list = $balanceModel->where('user_id',$user_id)->limit($offset,$limit)->select();
        foreach ($list as $key=>$val){
            if($val['type'] == 'in'){
                $list[$key]['type'] = "新增";
            }else{
                $list[$key]['type'] = "抵扣";
            }
            $list[$key]['create_time_txt'] = date("Y-m-d H:i",$val['create_time']);
        }
        $total = $balanceModel->where('user_id',$user_id)->count('id');

        $ret = array();
        $ret['total'] = $total;
        $ret['list'] = $list;
        $this->success('',$ret);
    }

}
